package com.emath.objects;

import java.util.Arrays;
import java.util.List;
import com.emath.shared.BigRational;
import com.emath.shared.Globals;

public class CurrentCard {
	public int cardID;
	public boolean tapped;

	public BigRational function;
	public BigRational mind;
	public BigRational strength;
	public BigRational charm;
	public BigRational legacy;
	public BigRational legend;
	public BigRational power;
	public BigRational mind1;
	public BigRational mind2;
	public BigRational mind3;
	public BigRational charm1;
	public BigRational charm2;
	public BigRational charm3;
	public BigRational strength1;
	public BigRational strength2;
	public BigRational strength3;
	public String cost;
	public String levelup, special, specialeffect, image, setName, movie;
	public String type1;
	public String type2;
	public String type3;
	public String levelup1;
	public String levelup2;
	public String levelup3;
	public String level;
	public String name;
	public String cardType;
	// Bitmap bitmap;

	/*
	 * private int rollDice() { int d1, d2; d1 = (int)((Math.random() * 6) + 1);
	 * d2 = (int)((Math.random() * 6) + 1); return (d1 + d2); }
	 */
	public CurrentCard(int id) {
		type1 = "";
		cardType = "";
		cardID = id;
		power = BigRational.valueOf("0");
		legacy = BigRational.valueOf("0");
		legend = BigRational.valueOf("0");
		tapped = false;
	}

	public CurrentCard(int id, Tuple attr, Tuple type, String c, Tuple levelup,
			Tuple m, Tuple ch, Tuple s, String cardname) {
		type1 = type.x;
		type2 = type.y;
		type3 = type.z;
		levelup1 = levelup.x;
		levelup2 = levelup.y;
		;
		levelup3 = levelup.z;
		mind1 = BigRational.valueOf(m.x);
		mind2 = BigRational.valueOf(m.y);
		mind3 = BigRational.valueOf(m.z);
		charm1 = BigRational.valueOf(ch.x);
		charm2 = BigRational.valueOf(ch.y);
		charm3 = BigRational.valueOf(ch.z);
		strength1 = BigRational.valueOf(s.x);
		strength2 = BigRational.valueOf(s.y);
		strength3 = BigRational.valueOf(s.z);
		cardType = "";
		cardID = id;
		cost = c;
		mind = BigRational.valueOf(attr.x);
		charm = BigRational.valueOf(attr.y);
		strength = BigRational.valueOf(attr.z);
		tapped = false; // The card can be tapped once it is put into play
						// anyway
		name = cardname;
		BigRational half = BigRational.getInstance(1, 2);
		BigRational third = BigRational.getInstance(1, 3);
		BigRational quarter = BigRational.getInstance(1, 4);

		if (type1.equals("Athlete")) {
			// function= (1/4)(mind) + (1/4)(charm) + (1/2)(strength)
			function = ((quarter.multiply(mind)).add(quarter.multiply(charm)))
					.add(half.multiply(strength));
			cardType = "Character";
		} else if (type1.equals("Student")) {
			// function= (1/2)(mind) + (1/4)(charm) + (1/4)(strength)
			function = ((half.multiply(mind)).add(quarter.multiply(charm)))
					.add(quarter.multiply(strength));
			cardType = "Character";
		} else if (type1.equals("Artist")) {
			// function= (1/4)(mind) + (1/2)(charm) + (1/4)(strength)
			function = ((quarter.multiply(mind)).add(half.multiply(charm)))
					.add(quarter.multiply(strength));
			cardType = "Character";
		} else if (type1.equals("Leader")) {
			// function= (1/3)(mind) + (1/3)(charm) + (1/3)(strength)
			function = ((third.multiply(mind)).add(third.multiply(charm)))
					.add(third.multiply(strength));
			cardType = "Character";
		} else {
			// Resource Card (or Undefined type)
			function = BigRational.valueOf("0");
			cardType = "Resource";
		}
		legacy = (mind.doubleValue() == 0) ? BigRational.valueOf("0") : (mind
				.add(function));
		legend = (function.doubleValue() > strength.doubleValue()) ? function
				: strength;
		power = (legacy.doubleValue() > legend.doubleValue()) ? legacy : legend;
	}

	// Copy Constructor
	public CurrentCard(CurrentCard c) {
		/*** Direct Copying ***/
		this.cardID = c.cardID;
		this.tapped = c.tapped;
		this.function = c.function;
		this.mind = c.mind;
		this.strength = c.strength;
		this.charm = c.charm;
		this.legacy = c.legacy;
		this.legend = c.legend;
		this.power = c.power;
		this.mind1 = c.mind1;
		this.mind2 = c.mind2;
		this.mind3 = c.mind3;
		this.charm1 = c.charm1;
		this.charm2 = c.charm2;
		this.charm3 = c.charm3;
		this.strength1 = c.strength1;
		this.strength2 = c.strength2;
		this.strength3 = c.strength3;
		this.cost = c.cost;
		this.levelup = c.levelup;
		this.special = c.special;
		this.specialeffect = c.specialeffect;
		this.image = c.image;
		this.setName = c.setName;
		this.name = c.name;
		this.movie = c.movie;
		this.type1 = c.type1;
		this.type2 = c.type2;
		this.type3 = c.type3;
		this.levelup1 = c.levelup1;
		this.levelup2 = c.levelup2;
		this.levelup3 = c.levelup3;
		this.level = c.level;
		this.cardType = c.cardType;
		// this.bitmap = c.bitmap;

		/*** Direct Copying -> Using setter/getter functions ***/
		/*
		 * setCardID(getCardID()); setTapped(getTapped());
		 * setFunction(getFunction()); setMind(getMind());
		 * setStrength(getStrength()); setCharm(getCharm());
		 * setLegacy(getLegacy()); setLegend(getLegend()); setPower(getPower());
		 * setMind1(getMind1()); setMind2(getMind2()); setMind3(getMind3());
		 * setCharm1(getCharm1()); setCharm2(getCharm2());
		 * setCharm3(getCharm3()); setStrength1(getStrength1());
		 * setStrength2(getStrength2()); setStrength3(getStrength3());
		 * setCost(getCost()); setLevelUp(getLevelUp());
		 * setSpecial(getSpecial()); setSpecialEffect(getSpecialEffect());
		 * setImage(getImage()); setSetName(getSetName()); setMovie(getMovie());
		 * setType1(getType1()); setType2(getType2()); setType3(getType3());
		 * setLevelUp1(getLevelUp1()); setLevelUp2(getLevelUp2());
		 * setLevelUp3(getLevelUp3()); setLevel(getLevel());
		 * setCardType(getCardType()); setBitmap(getBitmap());
		 */
	}

	public void tapCard() {
		tapped = true;
	}
	public void untapCard() {
		tapped = false;
	}

	/*** Getter Functions ***/
	public int getCardID() {
		return this.cardID;
	}
	public BigRational getFunction() {
		return this.function;
	}
	public String getMind() {
		return String.valueOf(mind);
	}
	public String getCharm() {
		return String.valueOf(charm);
	}
	public String getStrength() {
		return String.valueOf(strength);
	}
	public BigRational getLegacy() {
		return this.legacy;
	}
	public BigRational getLegend() {
		return this.legend;
	}
	public BigRational getPower() {
		return this.power;
	}
	public BigRational getMind1() {
		return this.mind1;
	}
	public BigRational getMind2() {
		return this.mind2;
	}
	public BigRational getMind3() {
		return this.mind3;
	}
	public BigRational getCharm1() {
		return this.charm1;
	}
	public BigRational getCharm2() {
		return this.charm2;
	}
	public BigRational getCharm3() {
		return this.charm3;
	}
	public BigRational getStrength1() {
		return this.strength1;
	}
	public BigRational getStrength2() {
		return this.strength2;
	}
	public BigRational getStrength3() {
		return this.strength3;
	}
	public String getCost() {
		return this.cost;
	}
	public String getLevelUp() {
		return this.levelup;
	}
	public String getSpecial() {
		return this.special;
	}
	public String getSpecialEffect() {
		return this.specialeffect;
	}
	public String getImage() {
		return this.image;
	}
	public String getName() {
		return this.name;
	}
	public String getSetName() {
		return this.setName;
	}
	public String getMovie() {
		return this.movie;
	}
	public String getType1() {
		return this.type1;
	}
	public String getType2() {
		return this.type2;
	}
	public String getType3() {
		return this.type3;
	}
	public String getLevelUp1() {
		return this.levelup1;
	}
	public String getLevelUp2() {
		return this.levelup2;
	}
	public String getLevelUp3() {
		return this.levelup3;
	}
	public int getLevel() {
		return Integer.parseInt(level);
	}
	public String getCardType() {
		return this.cardType;
	}
	// public Bitmap getBitmap(){
	// return this.bitmap;
	// }
	public boolean isTapped() {
		return tapped;
	}
	public boolean isCharCard() {
		return (cardType.equals("Character"));
	}
	public boolean isResCard() {
		return (cardType.equals("Resource"));
	}

	/*** Setter Functions ***/
	public void setCardID(int cardID) {
		this.cardID = cardID;
	}
	public void setFunction(BigRational function) {
		this.function = function;
	}
	public void setMind(BigRational mind) {
		this.mind = mind;
	}
	public void setCharm(BigRational charm) {
		this.charm = charm;
	}
	public void setStrength(BigRational strength) {
		this.strength = strength;
	}
	public void setLegacy(BigRational legacy) {
		this.legacy = legacy;
	}
	public void setLegend(BigRational legend) {
		this.legend = legend;
	}
	public void setPower(BigRational power) {
		this.power = power;
	}
	public void setMind1(BigRational mind1) {
		this.mind1 = mind1;
	}
	public void setMind2(BigRational mind2) {
		this.mind2 = mind2;
	}
	public void setMind3(BigRational mind3) {
		this.mind3 = mind3;
	}
	public void setCharm1(BigRational charm1) {
		this.charm1 = charm1;
	}
	public void setCharm2(BigRational charm2) {
		this.charm2 = charm2;
	}
	public void setCharm3(BigRational charm3) {
		this.charm3 = charm3;
	}
	public void setStrength1(BigRational strength1) {
		this.strength1 = strength1;
	}
	public void setStrength2(BigRational strength2) {
		this.strength2 = strength2;
	}
	public void setStrength3(BigRational strength3) {
		this.strength3 = strength3;
	}
	public void setCost(String cost) {
		this.cost = cost;
	}
	public void setLevelUp(String levelUp) {
		this.levelup = levelUp;
	}
	public void setSpecial(String special) {
		this.special = special;
	}
	public void setSpecialEffect(String specialEffect) {
		this.specialeffect = specialEffect;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setSetName(String setName) {
		this.setName = setName;
	}
	public void setMovie(String movie) {
		this.movie = movie;
	}
	public void setType1(String type1) {
		this.type1 = type1;
	}
	public void setType2(String type2) {
		this.type2 = type2;
	}
	public void setType3(String type3) {
		this.type3 = type3;
	}
	public void setLevelUp1(String levelup1) {
		this.levelup1 = levelup1;
	}
	public void setLevelUp2(String levelup2) {
		this.levelup2 = levelup2;
	}
	public void setLevelUp3(String levelup3) {
		this.levelup3 = levelup3;
	}
	public void setlevel(String level) {
		this.level = level;
		if (level.equals("1")) {
			mind = mind1;
			strength = strength1;
			charm = charm1;
		}
		if (level.equals("2")) {
			mind = mind2;
			strength = strength2;
			charm = charm2;
		}
		if (level.equals("3")) {
			mind = mind3;
			strength = strength3;
			charm = charm3;
		}
		BigRational half = BigRational.getInstance(1, 2);
		BigRational third = BigRational.getInstance(1, 3);
		BigRational quarter = BigRational.getInstance(1, 4);

		if (type1.equals("Athlete")) {
			// function= (1/4)(mind) + (1/4)(charm) + (1/2)(strength)
			function = ((quarter.multiply(mind)).add(quarter.multiply(charm)))
					.add(half.multiply(strength));
			cardType = "Character";
		} else if (type1.equals("Student")) {
			// function= (1/2)(mind) + (1/4)(charm) + (1/4)(strength)
			function = ((half.multiply(mind)).add(quarter.multiply(charm)))
					.add(quarter.multiply(strength));
			cardType = "Character";
		} else if (type1.equals("Artist")) {
			// function= (1/4)(mind) + (1/2)(charm) + (1/4)(strength)
			function = ((quarter.multiply(mind)).add(half.multiply(charm)))
					.add(quarter.multiply(strength));
			cardType = "Character";
		} else if (type1.equals("Leader")) {
			// function= (1/3)(mind) + (1/3)(charm) + (1/3)(strength)
			function = ((third.multiply(mind)).add(third.multiply(charm)))
					.add(third.multiply(strength));
			cardType = "Character";
		} else {
			// Resource Card (or Undefined type)
			function = BigRational.valueOf("0");
			cardType = "Resource";
		}
		legacy = (mind.doubleValue() == 0) ? BigRational.valueOf("0") : (mind
				.add(function));
		legend = (function.doubleValue() > strength.doubleValue()) ? function
				: strength;
		power = (legacy.doubleValue() > legend.doubleValue()) ? legacy : legend;

	}
	public void setCardType(String c) {
		this.cardType = c;
	}
	// public void setBitmap(Bitmap b){
	// this.bitmap = b;
	// }

	public void copyCardDataFromCardList() {
		Card cardObj = new Card();
		cardObj = Globals.libraryObj.getCardbyID("" + this.cardID);
		this.name = (cardObj.getCardName());
		this.cardType = (cardObj.getType());
		this.mind1 = (BigRational.valueOf(cardObj.getMind1()));
		this.mind2 = (BigRational.valueOf(cardObj.getMind2()));
		this.mind3 = (BigRational.valueOf(cardObj.getMind3()));
		this.charm1 = (BigRational.valueOf(cardObj.getCharm1()));
		this.charm2 = (BigRational.valueOf(cardObj.getCharm2()));
		this.charm3 = (BigRational.valueOf(cardObj.getCharm3()));
		this.strength1 = (BigRational.valueOf(cardObj.getStrength1()));
		this.strength2 = (BigRational.valueOf(cardObj.getStrength2()));
		this.strength3 = (BigRational.valueOf(cardObj.getStrength3()));
		this.cost = (cardObj.getCost());
		List<String> lvlUps = Arrays.asList(cardObj.getLevelUp().split(":"));
		this.levelup1 = (lvlUps.get(0));
		if (lvlUps.size() > 1)
			this.levelup2 = (lvlUps.get(1));
		if (lvlUps.size() > 2)
			this.levelup3 = (lvlUps.get(2));
		this.setName = (cardObj.getSetName());
		this.special = (cardObj.getSpecial());
		this.specialeffect = (cardObj.getSpecialEffect());
	}

}
